package com.jvyou.app.common.domain.model;

import com.jvyou.app.common.enums.ErrorShowType;
import com.jvyou.app.common.enums.HttpCodeEnum;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

@Setter
@Getter
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1242156298234542963L;

    private boolean success;
    private T data;
    private Integer errorCode;
    private String errorMessage;
    private Integer showType;

    // 构造方法
    public Result() {
    }

    public Result(boolean success, T data) {
        this.success = success;
        this.data = data;
    }

    public Result(boolean success, Integer errorCode, String errorMessage, ErrorShowType showType) {
        this.success = success;
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
        this.showType = showType.getCode();
    }

    public Result(boolean success, Integer errorCode, String errorMessage, Integer showType, T data) {
        this.success = success;
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
        this.showType = showType;
        this.data = data;
    }

    // 静态方法用于快速创建响应对象
    public static <T> Result<T> success(T data) {
        return new Result<>(true, 200, "操作成功", -1, data);
    }

    public static <T> Result<T> error(Integer errorCode, String errorMessage, ErrorShowType showType) {
        return new Result<>(false, errorCode, errorMessage, showType);
    }

    public static <T> Result<T> error(HttpCodeEnum httpCodeEnum, ErrorShowType showType) {
        return new Result<>(false, httpCodeEnum.getCode(), httpCodeEnum.getMsg(), showType);
    }

    public static <T> Result<T> error(HttpCodeEnum httpCodeEnum) {
        return new Result<>(false, httpCodeEnum.getCode(), httpCodeEnum.getMsg(), httpCodeEnum.getShowType());
    }

    public static <T> Result<T> error(HttpCodeEnum httpCodeEnum, String msg) {
        return new Result<>(false, httpCodeEnum.getCode(), msg, httpCodeEnum.getShowType());
    }
}
